﻿// <copyright file="SketchFlowAnalytics.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-11-30</date>
// <summary>SketchFlowAnalytics class definition</summary>

namespace Microsoft.WebAnalytics.SketchFlow
{
    using System;
    using System.ComponentModel.Composition;
    using System.Globalization;
    using System.Windows;
    using Microsoft.Expression.Prototyping.Navigation;
    using Microsoft.Expression.Prototyping.Workspace;
    using Microsoft.WebAnalytics;

    /// <summary>
    /// Use this to create the LayoutRoot for a SketchFlow application
    /// You should also add the WebAnalyticsService and service behaviors to the Application.ApplicationLifetimeObjects
    /// </summary>
    /// <remarks>
    /// <list type="table">
    /// <listheader>
    /// <term>AnalyticsEvent Property</term>
    /// <description>Description</description>
    /// </listheader>
    /// <item>
    /// <term>ActionValue</term>
    /// <description>ScreensAssembly.ScreenName</description>
    /// </item>
    /// <item>
    /// <term>HitType</term>
    /// <description>PageView</description>
    /// </item>
    /// <item>
    /// <term>Name</term>
    /// <description>CurrentScreenChanged</description>
    /// </item>
    /// <item>
    /// <term>NavigationState</term>
    /// <description>/Screen Description (Escaped)</description>
    /// </item>
    /// <item>
    /// <term>ObjectName</term>
    /// <description>Screen Description</description>
    /// </item>
    /// <item>
    /// <term>ObjectType</term>
    /// <description>NavigationViewModel</description>
    /// </item>
    /// </list>
    /// </remarks>
    public class SketchFlowAnalytics
    {
        /// <summary>
        /// Gets or sets the Log method
        /// </summary>
        [Import("Log")]
        public Action<AnalyticsEvent> Log { get; set; }

        /// <summary>
        /// Creates the RootVisual element in the Application_Startup event handler
        /// </summary>
        /// <param name="application">the new application instance in the Application_Startup event handler</param>
        /// <param name="playerWindow">a new SketchFlow Player Window</param>
        /// <returns>a Grid element that contains the PlayerWindow and the analytics behaviors</returns>
        public UIElement CreateRootVisual(Application application, PlayerWindow playerWindow)
        {
            CompositionInitializerEx.SatisfyImports(this);

            playerWindow.NavigationViewModel.CurrentScreenChanged += new EventHandler<ScreenChangedEventArgs>(this.OnCurrentScreenChanged);

            return playerWindow;
        }
        
        /// <summary>
        /// Current Screen changed event handler
        /// </summary>
        /// <param name="sender">the NavigationViewModel</param>
        /// <param name="e">the Screen changed event arguments</param>
        private void OnCurrentScreenChanged(object sender, ScreenChangedEventArgs e)
        {
            var navViewModel = sender as NavigationViewModel;

            this.Log(new AnalyticsEvent()
            {
                ActionValue = e.NewScreen,
                HitType = Data.HitType.PageView,
                Name = "CurrentScreenChanged",
                NavigationState = string.Format(CultureInfo.InvariantCulture, "/{0}", Uri.EscapeDataString(navViewModel.CurrentScreenDescription)),
                ObjectName = navViewModel.CurrentScreenDescription,
                ObjectType = sender.GetType().Name
            });
        }
    }
}
